﻿using AirlineBookingSystem.Flight.Core.Entities;
using AirlineBookingSystem.Flight.Core.Repositories;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AirlineBookingSystem.Flight.Infrastructure.Repositories
{
    public class FlightRepository : IFlightRepository
    {
        private readonly IDbConnection _connection;

        public FlightRepository(IDbConnection connection)
        {
            _connection = connection;
        }


        public async Task AddFlightAsync(Flights flight)
        {
            const string sql = @"
                INSERT INTO Flights(Id,FlightNumber,Origin,Destination,DepartureTime,ArrivalTime)
                VALUES(@Id,@FlightNumber,@Origin,@Destination,@DepartureTime,@ArrivalTime)";

            await _connection.ExecuteAsync(sql, flight);
        }

        public async Task DeleteFlightAsync(Guid id)
        {
            const string sql = "delete from flights where Id=@Id";
            await _connection.ExecuteAsync(sql, new { Id = id });
        }

        public async Task<IEnumerable<Flights>> GetFlightsAsync()
        {
            const string sql = "select * from Flights";
            return await _connection.QueryAsync<Flights>(sql);
        }
    }
}
